﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;

namespace Omega
{
    public partial class Form1 : Form
    {
        enum State{
            playing,
            stoped
        }

        private State currentState;
        private List<string> binaryValues;
        private List<float> values;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            chart1.Series[0].ChartType = SeriesChartType.FastLine;
             

        }

        private void button1_Click(object sender, EventArgs e)
        {
           timer1.Enabled = true;
           currentState = State.playing;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            // Fill series data
            double yValue = 0.0;
            Random random = new Random();
            yValue = yValue + (random.NextDouble() * 10.0 - 5.0);
            chart1.Series[0].Points.AddY(yValue);
            //chart1.ChartAreas[0].CursorX.Position = chart1.Series[0].Points[chart1.Series[0].Points.Count-1].XValue;
            //chart1.Width += 30;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            currentState = State.stoped;
        }

        private void button3_Click(object sender, EventArgs e)
        {


            String input = string.Empty;
            String fileName = String.Empty;
            String tempRead;

            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

         
            dialog.InitialDirectory = "C:";
            dialog.Title = "Load file";


            if (dialog.ShowDialog() == DialogResult.OK)
                fileName = dialog.FileName;
            if (fileName == String.Empty)
                return;

            StreamReader tr = new StreamReader(fileName);
            binaryValues = new List<string>();
            values = new List<float>();

            while ((tempRead = tr.ReadLine()) != null)
            {
                
                
                binaryValues.Add(tempRead);
                values.Add(byteToValue(tempRead));
            }

            // close the stream
            tr.Close();


            chart1.Series[0] = new Series();
            chart1.Series[0].ChartType = SeriesChartType.FastLine;
            chart1.Series[0].IsVisibleInLegend = false;

            foreach(float value in values) {
                chart1.Series[0].Points.AddY(value);
            }
        }

        private float byteToValue(string nbyte)
        {
            float temp = Convert.ToInt32(nbyte, 2);
            temp = temp / 255 * 10 -5;

            return temp;
        }

        private string valueToByte(double value)
        {
            double temp = (value + 5) / 10 * 255;
            string byteString = Convert.ToString((int)temp, 2);

            if (byteString.Length < 8)
            {
                while(byteString.Length < 8)
                {
                    byteString = "0" + byteString;
                }
            }

            return byteString;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            String input = string.Empty;
            String fileName = String.Empty;
            String tempRead;

            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";


            dialog.InitialDirectory = "C:";
            dialog.Title = "Save file";


            if (dialog.ShowDialog() == DialogResult.OK)
                fileName = dialog.FileName;
            if (fileName == String.Empty)
                return;


            TextWriter tw = new StreamWriter(fileName);

            for (int i = 0; i < chart1.Series[0].Points.Count; i++)
            {
                tw.WriteLine(valueToByte(chart1.Series[0].Points[i].YValues[0]));
            }


            tw.Close();
        }


    }
}
